<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class ArticleController extends HomeController {
    /* 文档模型频道页 */
	public function index(){
		/* 分类信息 */
		$category = $this->category();

		//频道页只显示模板，默认不读取任何内容
		//内容可以通过模板标签自行定制

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->display($category['template_index']);
	}

	/* 文档模型列表页 */
	public function lists($p = 1){
		/* 分类信息 */
		$category = $this->category();

		/* 获取当前分类列表 */
		$Document = D('Document');
		$list = $Document->page($p, $category['list_row'])->lists($category['id']);
		if(false === $list){
			$this->error('获取列表数据失败！');
		}

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('list', $list);
		$this->display($category['template_lists']);
	}

	/* 文档模型详情页 */
	public function detail($id = 0, $p = 1){
		/* 标识正确性检测 */
		if(!($id && is_numeric($id))){
			$this->error('文档ID错误！');
		}

		/* 页码检测 */
		$p = intval($p);
		$p = empty($p) ? 1 : $p;

		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->detail($id);
		if(!$info){
			$this->error($Document->getError());
		}

		/* 分类信息 */
		$category = $this->category($info['category_id']);

		/* 获取模板 */
		if(!empty($info['template'])){//已定制模板
			$tmpl = $info['template'];
		} elseif (!empty($category['template_detail'])){ //分类已定制模板
			$tmpl = $category['template_detail'];
		} else { //使用默认模板
			$tmpl = 'Article/'. get_document_model($info['model_id'],'name') .'/detail';
		}

		/* 更新浏览数 */
		$map = array('id' => $id);
		$Document->where($map)->setInc('view');

		/* 模板赋值并渲染模板 */
		$this->assign('category', $category);
		$this->assign('info', $info);
		$this->assign('page', $p); //页码
		$this->display($tmpl);
	}

	/* 文档分类检测 */
	private function category($id = 0){
		/* 标识正确性检测 */
		$id = $id ? $id : I('get.category', 0);
		if(empty($id)){
			$this->error('没有指定文档分类！');
		}

		/* 获取分类信息 */
		$category = D('Category')->info($id);
		if($category && 1 == $category['status']){
			switch ($category['display']) {
				case 0:
					$this->error('该分类禁止显示！');
					break;
				//TODO: 更多分类显示状态判断
				default:
					return $category;
			}
		} else {
			$this->error('分类不存在或被禁用！');
		}
	}
	/*自己写的方法*/
	public function news(){
	    /*重要通知*/
	    $cate_id =I("get.cate_id",155,"int");
	    $this->meta($cate_id);
        $document = M("document");
        $community_detail =session("community_detail");
        $map =[
            'ot_document_article.community_ids'=>array("like",'%'.$community_detail['id'].'%'),
            'ot_document.status' =>1,
            'ot_document.pid' => 0,
            'ot_document.category_id' =>$cate_id
        ];
        $lists = $document->join("ot_document_article on ot_document.id =ot_document_article.id")->field("ot_document.id,ot_document.title,ot_document.create_time,ot_document_article.content")->where($map)->order("level desc,id desc")->select();
        //print_r($lists);exit;
        $this->assign("lists",$lists);
        $this->display();
    }
    public function news_detail(){
        $id =I("get.id",0,"int");
        $data=$this->document->detail($id);
        $this->meta($data['category_id']);
        $this->assign("data",$data);
        $this->display();
    }
    public function cs(){
        $meta=array(
            'meta_title'=>C("WEB_SITE_TITLE"),
            'keywords'  =>C("WEB_SITE_KEYWORD"),
            'description'=>C("WEB_SITE_DESCRIPTION"),
            'template_index'=>"Index/index",
        );
        $this->assign("meta",$meta);
        $this->assign("active",'lxwm');
        $this->display('sc');
    }
    public function upload(){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/phone/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();

        /*if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功


        }*/
        $imgs=[];
        foreach ($info as $key=>$val){
            $imgs[] =$val['savepath'].$val['savename'];
        }
        $community_detail =session("community_detail");
        $data=[
            'cate_id'=>I("post.cate_id",0,"int"),
            'name' =>I("post.name","","addslashes"),
            'tel'  =>I("post.tel","","addslashes"),
            'content'  =>I("post.content","","addslashes"),
            'create_time'=>time(),
            'community_id'=>$community_detail['id'],
            'imgs'=>json_encode($imgs)
        ];
        $contact =M("contact");
        if($contact->add($data)){
            echo "<script>alert('提交成功');window.location.href='".U("Article/cs")."';</script>";
        }
    }


}
